package com.java.function;

import com.java.function.bean.Apple;
import com.java.function.service.ApplePredicate;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.function.Predicate;

public class FilterApples {
    static List<Apple> repos = Arrays.asList(
            new Apple(80,"green"),
            new Apple(120,"green"),
            new Apple(150,"red"),
            new Apple(70,"green"),
            new Apple(95,"red")
    );

    public static void main(String[] args) {
        System.out.println(filterGreenApples());
        System.out.println(filterApples(apple -> "green".equals(apple)));
        System.out.println(filterWeightApples());
        System.out.println(filterApples(apple -> apple.getWeitght() > 100));
        System.out.println(filterApples2(apple -> apple.getWeitght()>100));
    }

    private static  List<Apple> filterGreenApples(){
        List<Apple> result = new ArrayList<>();
        for (Apple apple : repos) {
            if ("green".equals(apple.getColor())){
                 result.add(apple);
            }
        }
        return result;
    }

    private static  List<Apple> filterWeightApples(){
        List<Apple> result = new ArrayList<>();
        for (Apple apple : repos) {
            if (apple.getWeitght()>100){
                result.add(apple);
            }
        }
        return result;
    }

    private static  List<Apple> filterApples(ApplePredicate applePredicate){
        List<Apple> result = new ArrayList<>();
        for (Apple apple : repos) {
            if (applePredicate.test(apple)){
                result.add(apple);
            }
        }
        return result;
    }


    /**
     *
     *Predicate 接收一个T对象，返回boolean
     *Consumer  接收一个T对象，无返回
     *Function  接收一个T对象，返回R对象
     *Supplier  提供T对象，不接收值
     */
    private static  List<Apple> filterApples2(Predicate<Apple> predicate){
        List<Apple> result = new ArrayList<>();
        for (Apple apple : repos) {
            if (predicate.test(apple)){
                result.add(apple);
            }
        }
        return result;
    }
}
